import './assets/main.css'

import { createApp } from 'vue'
import { createStore } from 'vuex'
import App from './App.vue'

import router from './router/index'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { delCookie, setCookie } from './utils/cookie.utils'



Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

// 创建共享状态库
const store = createStore({
    state: {
        token: null,
        user: null,
        model: "ChatGLM3-6B",
        temperature: 0.5,
    },
    mutations: {
        updateToken(state, payload) {
            state.token = payload.token
            delCookie("token")
            setCookie("token", payload.token)
        },
        updateUser(state, payload) {
            state.user = payload
            delCookie("user")
            setCookie("user", JSON.stringify(state.user))
        },
        updateNickname(state, payload) {
            state.user["nickname"] = payload
            delCookie("user")
            setCookie("user", JSON.stringify(state.user))
        },
        updatePassword(state, payload) {
            state.user["password"] = payload
            delCookie("user")
            setCookie("user", JSON.stringify(state.user))
        },
        updateAvatar(state, payload) {
            state.user["avatar"] = payload
            delCookie("user")
            setCookie("user", JSON.stringify(state.user))
        },
        updateModel(state, payload) {
            state.model = payload
            delCookie("model")
            setCookie("model", payload)
        },
        updateTemperature(state, payload) {
            state.temperature = payload
            delCookie("temperature")
            setCookie("temperature", payload.toString())
        }
    },
    getters: {
        getAvatar(state) {
            return state.user["avatar"]
        },
        getNickname(state) {
            return state.user["nickname"]
        },
        getUser(state) {
            return state.user
        }
    }
})

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.use(store)
app.use(router)
app.use(ElementPlus)
app.mount('#app')
